package atguigu.filter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Date:2020/9/16
 * Author:ybc
 * Description:
 */
public class LoginFilter extends HttpFilter {

    @Override
    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if(user == null){
            //未登入
            response.sendRedirect(request.getContextPath() + "/");
        }else{
            //登入了 放行
            chain.doFilter(request,response);
        }
    }
}
